<?php

namespace app\modules\mall\controllers;

use Yii;
use app\modules\mall\models\Goods;
use app\modules\mall\models\Order;
use app\modules\mall\models\Index;
use app\modules\mall\models\Cart;
use app\modules\mall\models\Welfare;
class IndexController extends \yii\web\Controller
{

    //商品首页每页上新数
    const INDEX_SIZE = 10;
    
    /**
     * 商城首页
     * GET
     */
    public function actionIndex() {
        $data = [];
        $data['page']   = $this->_getPage();
        $data['size']   = self::INDEX_SIZE;
        $data['date']   = date("Y-m-d");
        $indexModel     = new Index();
        $goodsModel     = new Goods();
        $cartModel      = new Cart();
        $welfareModel   = new Welfare();
        
        $arr = [];
        $goodsList = $indexModel->getList($data);
        foreach($goodsList as $key=>$value) {
            $goodsInfo = $goodsModel->getGoodsInfo(['goodsid'=>$value['id']]);
            $goodsList[$key]['name']            = htmlspecialchars_decode($goodsList[$key]['name']);
            $goodsList[$key]['cid']             = $goodsInfo['cid'];
            $goodsList[$key]['price']           = $goodsInfo['price'];
            $goodsList[$key]['real_price']      = $goodsInfo['real_price'];
            $goodsList[$key]['unit']            = $goodsInfo['unit'];
            $goodsList[$key]['source']          = $goodsInfo['source'];
            $goodsList[$key]['sales_3day']      = $goodsInfo['sales_3day'];
            $goodsList[$key]['tax']             = $goodsInfo['tax'];
            $goodsList[$key]['isShippingFree']  = $goodsModel->isGoodsLogisticsFree($goodsInfo['tpl_id']);
        }
        $arr['goodsList']       = $goodsList;
        $goodsPicList = $indexModel->getPicList($data);
        if(empty($goodsPicList)) {
            $arr['goodsPicList'] = null;
        } else {
            $arr['goodsPicList'] = $goodsPicList;
        }
        $advList = $indexModel->getAdvlist($data);
        if(empty($advList)) {
            $arr['advList'] = null;
        } else {
            $arr['advList'] = $advList;
        }
        $welfareList = $welfareModel->index();
        if(empty($welfareList)) {
            $arr['welfareList'] = null;
        } else {
            $arr['welfareList'] = $welfareList;
        }
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $cartNum = $cartModel->getCartNum($uid);
            $arr['cartNum'] = $cartNum>0 ? $cartNum : "0"; //购物车数量
            $arr['hasMsg'] = $this->redis->get($uid . Yii::$app->params['rediskeys']['user_news']) > 0 ? '1' : '0';
        } else {
            $arr['cartNum'] = '0';
            $arr['hasMsg'] = '0';
        }
        $this->_exit(0, $arr);
    }
    /**
     * 商品列表
     * GET
     */
    public function actionList() {
        //sorts: 1综合，2销量，3价格(降序)，4价格(升序)
        $data = [];
        $sorts = trim(Yii::$app->request->get('sorts'));
        $data['sorts'] = (int)$sorts;
        $data['page'] = $this->_getPage();
        $data['size'] = 10;
        if(!is_numeric($data['sorts']) || $data['sorts']<1 || $data['sorts']>4) {
            $this->_exit(-1);
        } 
        $model = new Goods();
        
        $list = $model->getGoodslist($data);
        
        //得到商品的商家名
        foreach($list as $key=>$value) {
            $list[$key]['shopname'] = $model->getShopName($value['sid']);
            //是否包邮: 1-买家承担运费 2:卖家承担运费
            $list[$key]['isShippingFree'] = $model->isGoodsLogisticsFree($value['tpl_id']);
        }
        $this->_exit(0, $list);
    }
    /**
     * 商品详情
     * GET
     */
    public function actionGoodsdetail() {
        $data = [];
        
        $data['goodsid']    = trim(Yii::$app->request->get('goodsid', 0));
        if(!$data['goodsid'] || !intval($data['goodsid'])) {
            $this->_exit(-1);
        }
        $model          = new Goods();
        $cartModel      = new Cart();
        $welfareModel   = new Welfare();
        
        $info = $model->getGoodsInfo($data);
        $info['name'] = htmlspecialchars_decode($info['name']);
        if(empty($info)) {
            $this->_exit(40010);
        } 
        $shopInfo = $model->getShopInfo($info['sid']);
        $info['shopname']       = $shopInfo['shop_name'];
        $info['shopContent']    = $shopInfo['content'];
        $info['tel']            = $shopInfo['tel_phone'];
        $info['isShippingFree'] = $model->isGoodsLogisticsFree($info['tpl_id']);
        $info['maxPrice']       = $model->getGoodsMaxPrice($data['goodsid']);
        if(!$info['maxPrice'])
            $info['maxPrice'] = $info['price'];
        $info['minPrice'] = $model->getGoodsMinPrice($data['goodsid']);
        if(!$info['minPrice'])
            $info['minPrice'] = $info['price'];
        $info['location'] = $this->ip2Location();
        
        $welfareInfo = $welfareModel->detail($data['goodsid']);
        if(empty($welfareInfo)) {
            $welfareInfo = null;
        } else { 
            $welfareInfo['joinNum'] = $welfareModel->getJoinNum($welfareInfo['id']);
            if($welfareInfo['condition']) {
                $res = $this->cget(Yii::$app->params['mall_domain'] . 'communityv1/expert/getexpertname?expert_area_id=' . $welfareInfo['condition']);
                $res = json_decode($res, true);
                $welfareInfo['expertName'] = $res['expert_name'];
            } else {
                $welfareInfo['expertName'] = null;
            }
            
            $welfareInfo['endtime'] -= time(); 
        }
        
        $info['welfareInfo'] = $welfareInfo;
        
        //查看商品评论信息
        $tmp = [];
        $tmp['goods_id'] = $data['goodsid'];
        $tmp['type'] = 0;
        $tmp['page'] = 1;
        $tmp['size'] = 1;
        $comment = $model->getCommentList($tmp);
        if(!empty($comment)) {
            $uid = $comment[0]['uid'];
            $user = $this->cget(Yii::$app->params['passport_domain'].'ucenter/finduserinformation?uid='.$uid);
            $user = json_decode($user,true);
            $comment[0]['avatar'] = $user['data']['avatar'];
            $comment[0]['username'] = $user['data']['nickname'];
            $info['comment'] = $comment[0];
        } else {
            $info['comment'] = null;   
        }
        
        //商品是否收藏
        if($this->isLogin()) {
            $data['uid']        = $this->_getUid();
            $data['type']       = 0;
            $info['isCollect'] = $model->isCollect($data);
            $info['cartNum'] = $cartModel->getCartNum($data['uid']);
            $info['hasMsg'] = $this->redis->get($data['uid'] . Yii::$app->params['rediskeys']['user_news']) > 0 ? '1' : '0';
        } else {
            $info['isCollect'] = '0';
            $info['cartNum'] = '0';
            $info['hasMsg'] = '0';
        }
        $sku = $model->hasSku($data);
        if(empty($info['picids'])) {
            $info['picids'] = $info['main_pic'];
        } else {
            $info['picids'] = $info['main_pic'] . ',' . $info['picids'];
        }
        $arr = [];
        if($sku) {
            $colors = $model->getSkucolor($data);
            $arr['color'] = [];
            foreach($colors as $key=>$value) {
                $arr['color'][$key]['color'] = $value['color'];
                $arr['color'][$key]['exist'] = $model->existColorSize($data['goodsid'], 1, $value['color']);
            }
        
            $sizes = $model->getSkusize($data);
            $arr['size'] = [];
            foreach($sizes as $key=>$value) {
                $arr['size'][$key]['size'] = $value['size'];
                $arr['size'][$key]['exist'] = $model->existColorSize($data['goodsid'], 2, $value['size']);
            }
            $arr['skulists'] = $model->getSkulist($data);
            $info['skulist'] = $arr;
        } else {
            $info['skulist'] = null;
        }
        
        if(empty($info)) {
            $this->_exit(40010);
        } else {
            $this->_exit(0, $info);
        }
    }
    /**
     * 产品参数
     * GET
     */
    public function actionParameter() {
        $data = [];
        $data['goodsid'] = trim(Yii::$app->request->get('goodsid', 0));
        if(!$data['goodsid'] || !intval($data['goodsid'])) {
            $this->_exit(-1);
        }
        $model = new Goods();
        
        $parameter = $model->getParameter($data);
        $this->_exit(0, ["parameter"=>$parameter]);
    }
    /**
     * 图文详情
     * GET
     */
    public function actionDetails() {
        $data = [];
        $data['goodsid']  = trim(Yii::$app->request->get('goodsid', 0));
        if(!$data['goodsid'] || !intval($data['goodsid'])) {
            $this->_exit(-1);
        }
        $model = new Goods();
        
        $details = $model->getDetails($data);
        $this->_exit(0, ["details" => $details]);
    }
    /**
     * 得到商品sku列表
     * GET
     */
    public function actionSkulist() {
        $data = [];
        $data['goodsid'] = trim(Yii::$app->request->get('goodsid', 0));
        if(!$data['goodsid'] || !intval($data['goodsid'])) {
            $this->_exit(-1);
        }
        $model = new Goods();
        
        $sku = $model->hasSku($data);
        
        if($sku) {
            $colors = $model->getSkucolor($data);
            $arr['color'] = [];
            foreach($colors as $key=>$value) {
                $arr['color'][$key]['color'] = $value['color'];
                $arr['color'][$key]['exist'] = $model->existColorSize($data['goodsid'], 1, $value['color']);
            }
            
            $sizes = $model->getSkusize($data);
            $arr['size'] = [];
            foreach($sizes as $key=>$value) {
                $arr['size'][$key]['size'] = $value['size'];
                $arr['size'][$key]['exist'] = $model->existColorSize($data['goodsid'], 2, $value['size']);
            }
            //$arr['color']   = $model->getSkucolor($data);
            //$arr['size']    = $model->getSkusize($data);
            $arr['skulist'] = $model->getSkulist($data);
            $this->_exit(0, $arr);
        } else {
            $this->_exit(0, []);
        }  
    }
    /**
     * 商品搜索
     * POST
     */
    public function actionSearchlist() {
        $data = [];
        $data['desc'] = trim(Yii::$app->request->post('desc'));
        if(!$data['desc'] || $data['desc'] == '*') {
            $this->_exit(40011);
        }
        $data['page'] = $this->_postPage();
        $data['size'] = 10;
        $model = new Goods();
        $arr = [];
        $list = $model->getSearchList($data);
        foreach($list as $key=>$value) {
            $tmp = [];
            $tmp['id']          = $value['id'];
            $tmp['name']        = htmlspecialchars_decode($value['name']);
            $tmp['main_pic']    = $value['main_pic'];
            $tmp['price']       = $value['price'];
            $tmp['real_price']  = $value['real_price'];
            $tmp['unit']        = $value['unit'];
            $tmp['inventory']   = $value['inventory'];
            $tmp['sales']       = $value['sales'];
            $tmp['sales_3day']  = $value['sales_3day'];
            $tmp['tax']         = $value['tax'];
            $tmp['source']      = $value['source'];
            $tmp['is_delete']   = $value['is_delete'];
            $tmp['is_up_down']  = $value['is_up_down'];
            $tmp['shopname']    = $model->getShopName($value['sid']);
            $tmp['isShippingFree'] = $model->isGoodsLogisticsFree($value['tpl_id']);
            //得到收藏商品的详细信息
            $list[$key]['shopname'] = $model->getShopName($value['sid']);
            $list[$key]['isShippingFree'] = $model->isGoodsLogisticsFree($value['tpl_id']);
            $arr['goodsList'][] = $tmp;
        }
        //$arr['goodsList'] = $list;
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $arr['hasMsg'] = $this->redis->get($uid . Yii::$app->params['rediskeys']['user_news']) > 0 ? '1' : '0';
        } else {
            $arr['hasMsg'] = '0';
        }
        $this->_exit(0, $arr);
    }
    /**
     * 商品热门搜索
     */
    public function actionHotsearch() {
        $arr = [];
        $arr['list'] = ['口香糖','方便面','雪碧','薯片','吧台椅','煮蛋器','双人沙发','滤水壶','吸尘器','沙发','躺椅','扶手椅','拉杆箱'];
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $arr['hasMsg'] = $this->redis->get($uid . Yii::$app->params['rediskeys']['user_news']) > 0 ? '1' : '0';
        } else {
            $arr['hasMsg'] = '0';
        }
        $this->_exit(0, $arr);
    }
    /**
     * 添加评论
     * POST
     */
    public function actionAddcomment() {
        $data = [];
        if($this->isLogin()) {
            $data['uid'] = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        
        $data['orderid']   = trim(Yii::$app->request->post('orderid'));
        if(!is_numeric($data['orderid'])) {
            $this->_exit(-1);
        }
        $data['goods_id']   = trim(Yii::$app->request->post('goodsid'));
        $data['skuid']      = trim(Yii::$app->request->post('skuid', 0));
        $data['color']      = trim(Yii::$app->request->post('color'));
        $data['type']       = trim(Yii::$app->request->post('types', 0));
        $data['size']       = trim(Yii::$app->request->post('size'));
        $data['content']    = trim(Yii::$app->request->post('content'));
        if(!$data['goods_id'] || !intval($data['goods_id'])) {
            $this->_exit(-1);
        }
        $model = new Goods();
        $order = new Order();
        if($order->hasOrder(0, $data['uid'], $data['orderid'], $data['goods_id']) && $model->hasGoods($data['goods_id'])) {
            $res = $model->addComment($data);
            if($res) {
                $order->setCommentState($data['orderid']);
                $this->_exit(0);
            } else {
                $this->_exit(40007);
            }
        } else {
            $this->_exit(-1);
        }
    }
    /**
     * 批量添加评价
     * POST
     */
    public function actionAddcomments() {
        $data = [];
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        
        $orderid = trim(Yii::$app->request->post('orderid'));
        
        $comments = trim(Yii::$app->request->post('comments'));
        if(!is_numeric($orderid) || !$comments) {
            $this->_exit(-1);
        }
        $type=Yii::$app->request->post('types' ,0);
        $comments = json_decode($comments, 1);
        $model = new Goods();
        $order = new Order();
        foreach($comments as $comment) {
            $data = [];
            if(!$comment['goodsid'] || !is_numeric($comment['goodsid'])) {
                $this->_exit(-1);
            }
            if(trim($comment['content'])) {
                $data['uid']        = $uid;
                $data['orderid']    = $orderid;
                $data['goods_id']   = $comment['goodsid'];
                $data['type']       = $type;
                if(!isset($comment['skuid'])) {
                    $comment['skuid'] = 0;
                }
                $data['skuid']      = $comment['skuid'];
                
                if(!is_numeric($data['skuid'])) {
                    $this->_exit(-1);
                }
                if($comment['skuid']) {
                    $skuInfo = $model->getSkuinfo($comment['skuid'], $comment['goodsid']);
                    if(empty($skuInfo)) {
                        $this->_exit(-1);
                    }
                    $data['color']      = $skuInfo['color'];
                    $data['size']       = $skuInfo['size'];
                } else {
                    $data['color']  = '';
                    $data['size']   = '';
                }
                $data['content']    = $comment['content'];
                if($order->hasOrder(0, $data['uid'], $data['orderid'], $data['goods_id']) && $model->hasGoods($data['goods_id'])) {
                    $res = $model->addComment($data);
                    if(!$res) {
                        $this->_exit(40007);
                    }
                } else {
                    $this->_exit(-1);
                }
            }
            
        }
        $order->setCommentState($orderid);
        $this->_exit(0);
    }
    /**
     * 查看评论列表
     * GET
     */
    public function actionCommentlist() {
        $data = [];
        $data['goods_id'] = trim(Yii::$app->request->get('goodsid'));
        $data['page'] = $this->_getPage();
        $data['size'] = 10;
        $data['type'] = Yii::$app->request->get('type', 0);
        $model = new Goods();
        $welfareModel = new Welfare();
        
        if(!$data['goods_id'] || !is_numeric($data['goods_id']) || !$model->hasGoods($data['goods_id'])) {
            $this->_exit(-1);
        } 
        
        $commentList = $model->getCommentList($data);
        $commentNum = $model->getCommentNum($data);
        
        $arr = [];
        $arr['num'] = $commentNum;
        foreach($commentList as $key=>$list) {
            $uid = $list['uid'];
            $user = $this->cget(Yii::$app->params['passport_domain'].'ucenter/finduserinformation?uid='.$uid);
            $user = json_decode($user,true);
            $commentList[$key]['avatar'] = $user['data']['avatar'];
            $commentList[$key]['username'] = $user['data']['nickname'];
        }
        $arr['list'] = $commentList;
        
        $comment = $welfareModel->getComment($data['goods_id']);
        if($comment) {
            $comment['url'] = 'houpixapp://topic/' . $comment['cid'];
            $arr['officialComment'] = $comment;
            $arr['num'] += 1;
        } else {
            $arr['officialComment'] = null;
        }
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $arr['hasMsg'] = $this->redis->get($uid . Yii::$app->params['rediskeys']['user_news']) > 0 ? '1' : '0';
        } else {
            $arr['hasMsg'] = '0';
        } 
        $this->_exit(0, $arr);
    }
    /**
     * 收藏商品/取消收藏商品
     * POST
     */
    public function actionGoodscollect() {
        $data = [];
        if($this->isLogin()) {
            $data['uid'] = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $model = new Goods();
        $data['goodsid'] = trim(Yii::$app->request->post('goodsid'));
        if(!$data['goodsid'] || !intval($data['goodsid']) || !$model->hasGoods($data['goodsid'])) {
            $this->_exit(-1);
        }
        $data['type']= trim(Yii::$app->request->post('type',0));
        $isCollect = $model->isCollect($data);
        if($isCollect) {
            $res = $model->delCollectGoods($data);
        } else {
            $res = $model->collectGoods($data);
        }
        if($res) {
            $this->_exit(0);
        } else {
            $this->_exit(40008);
        }
    }
    /**
     * 得到用户收藏列表
     * GET
     */
    public function actionGoodscollectlist() {
        $data = [];
        if($this->isLogin()) {
            $data['uid'] = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        
        $data['page'] = $this->_getPage();
        $data['size'] = 10;
       
        $model = new Goods();
        
        $list = $model->getCollectList($data);
        foreach($list as $key=>$value) {
            //得到收藏商品的详细信息
            $tmp = [];
            $goodsinfo = $model->getGoodsInfo(['goodsid'=>$value['goods_id']]);
            $tmp['id']          = $goodsinfo['id'];
            $tmp['name']        = htmlspecialchars_decode($goodsinfo['name']);
            $tmp['main_pic']    = $goodsinfo['main_pic'];
            $tmp['price']       = $goodsinfo['price'];
            $tmp['real_price']  = $goodsinfo['real_price'];
            $tmp['unit']        = $goodsinfo['unit'];
            $tmp['inventory']   = $goodsinfo['inventory'];
            $tmp['sales']       = $goodsinfo['sales'];
            $tmp['sales_3day']  = $goodsinfo['sales_3day'];
            $tmp['tax']         = $goodsinfo['tax'];
            $tmp['source']      = $goodsinfo['source'];
            $tmp['is_delete']   = $goodsinfo['is_delete'];
            $tmp['is_up_down']  = $goodsinfo['is_up_down'];
            $tmp['shopname']    = $model->getShopName($goodsinfo['sid']);
            $tmp['isShippingFree'] = $model->isGoodsLogisticsFree($goodsinfo['tpl_id']);
            $list[$key] = $tmp;
        }
        $this->_exit(0, $list);
    }
    /**
     * 批量删除商品收藏
     * POST
     */
    public function actionDelgoodscollectlist() {
        $data = [];
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        
        $goodsids   = trim(Yii::$app->request->post('goodsids'));
        $type       = trim(Yii::$app->request->post('type', 0));
        if(!intval($goodsids)) {
            $this->_exit(-1);
        }
        $goodsids   = explode(',', $goodsids);
        $model = new Goods();
        foreach($goodsids as $goodsid) {
            $tmp = [];
            $tmp['uid']     = $uid;
            $tmp['goodsid'] = $goodsid;
            $tmp['type']    = $type;
            if(!is_numeric($goodsid) || !$model->hasGoods($goodsid)) {
                $this->_exit(-1);
            }
            $flag = $model->isCollect($tmp);
            if(!$flag) {
                $this->_exit(40015);
            }
        }
        foreach($goodsids as $goodsid) {
            $tmp = [];
            $tmp['uid']     = $uid;
            $tmp['goodsid'] = $goodsid;
            $tmp['type']    = $type;
            $res = $model->delCollectGoods($tmp);
            if(!$res) {
                $this->_exit(40009);
            }
        }
        $this->_exit(0);   
    }
    /**
     * 得到用户当前城市
     */
    public function actionGetlocation() {
        $city = $this->ip2Location();
        $this->_exit(0, ['city'=>$city]);
    }
    /**
     * 获取商家信息
     * */
    public function actionGetshopinfo()
    {
        $sid    = trim(Yii::$app->request->get('sid', 0));
        if(!$sid || !intval($sid)) {
            $this->_exit(-1);
        }
        $model = new Goods();
        
        $Info = $model->getShopInfo($sid);
        $this->_exit(0,$Info);
    }
    /**
     * 获取是卖家包邮还是买家付邮费
     * */
    public function actionIsshopfree(){
        $data = [];
        
        $data['goodsid']    = trim(Yii::$app->request->get('goodsid', 0));
        if(!$data['goodsid'] || !intval($data['goodsid'])) {
            $this->_exit(-1);
        }
        $model = new Goods();
        
        $info = $model->getGoodsInfo($data);
        $is =$model->isGoodsLogisticsFree($info['tpl_id']);
         $this->_exit(0,$is);
    }
    
    /**
     * 申请参加福利社活动
     * GET
     */
    public function actionApplyfor() {
        $data = [];
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $wid = $this->_validateNum(Yii::$app->request->get('wid'));
        
        //判断用户是否参加过该活动
        $welfareModel = new Welfare();
        if($welfareModel->isExist($wid)) {
            $isApply = $welfareModel->usertimes($uid, $wid);
            if($isApply) {
                $this->_exit(40027);
            } else {
                $this->_exit(0);
            }
        } else {
            $this->_exit(40030);
        }
    }
    /**
     * 抽奖计算
     * GET
     */
    public function actionWelfarelottery() {
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $wid = $this->_validateNum(Yii::$app->request->get('wid'));
        $welfareModel = new Welfare();
        $welfareInfo = $welfareModel->getInfo($wid);
        if(empty($welfareInfo)) {
            $this->_exit(40030);
        }
        
        $isApply = $welfareModel->usertimes($uid, $wid);
        if($uid == 23) $isApply=0;
        if(!$isApply) {
            //判断用户资格
            $lucknum = $welfareModel->lucknum($wid);
            $count = count(explode(',', $welfareInfo['winner'])) + $lucknum;
            $totalNum = $welfareInfo['pricenum'];
            if($count == $totalNum) {
                $data['uid']        = $uid;
                $data['createtime'] = time();
                $data['wid']        = $wid;
                $data['is_lucky']   = 0;
                $welfareModel->insertuser($data);
                $this->_exit(40031);
            }
            $flag = 0;
            if($welfareInfo['condition'] > 0) {
                $res = $this->cget(Yii::$app->params['mall_domain'] . 'communityv1/expert/checkuserexpert?expert_area_id=' . $welfareInfo['condition'] . '&uid=' . $uid);
                $res = json_decode($res, true);
                if($res['code'] > 0) {
                    $flag = 1;
                } else {
                    $ret = [];
                    $ret['expertName'] = $res['expert_name'];
                    $ret['url'] = 'houpixapp://topic/123'; 
                    $this->_exit(40028, $ret);
                }
            } elseif($welfareInfo['condition'] == 0) {
                $flag = 1;
            }
            
            if($flag == 1) {
                $mobile = $_SESSION['user']['mobile_num'];
                $probability = $welfareInfo['probability'];
                $rand = mt_rand(1, 100);
                if(in_array($mobile, explode(',', $welfareInfo['winner']))) {
                    $rand = 1;
                }
                $data['uid']=$uid;
                $data['createtime']=time();
                $data['wid']=$wid;
                
                //@todo 修改抽奖算法
                if($rand <= $probability) {
                    $data['is_lucky'] = 1;
                } else {
                    $data['is_lucky'] = 0;
                }
                $welfareModel->insertuser($data);
                $ret = [];
                if($data['is_lucky']) {
                    $ret['url'] = 'houpixapp://webview/http://share.houpix.com/eula/zhongcechoujiang/';
                    $ret['msg'] = Yii::$app->params['lucky'];
                    $this->_exit(0, $ret);
                } else {
                    $this->_exit(40031);
                }
            } else {
                $this->_exit(-1);
            }
        } else {
            $this->_exit(40027);
        }         
    }
    /**
     * 同意众测福利社协议
     * GET
     */
    public function actionWelfareagree() {
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $wid = $this->_validateNum(Yii::$app->request->get('wid'));
        $welfareModel = new Welfare();
        $data['uid'] = $uid;
        $data['wid'] = $wid;
        $data['is_agree'] = 1;
        $res = $welfareModel->agreeProc($data);
        if($res) {
            $this->_exit(0);
        } else {
            $this->_exit(40029);
        }
    }
    /**
     * 放弃福利社领奖
     * GET
     */
    public function actionWelfareabandon() {
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $wid = $this->_validateNum(Yii::$app->request->get('wid'));
        $welfareModel = new Welfare();
        $data = [];
        $data['uid'] = $uid;
        $data['wid'] = $wid;
        $res = $welfareModel->updateState($data);
        if($res) {
            $this->_exit(0);
        } else {
            $this->_exit(40029);
        }
    }
    
    /**
     * 福利社详情
     * GET
     */
    public function actionWelfaredetail() {
        
        $goodsid    = trim(Yii::$app->request->get('goodsid', 0));
        if(!$goodsid || !intval($goodsid)) {
            $this->_exit(-1);
        }
        $welfareModel   = new Welfare();
        $welfareInfo = $welfareModel->detail($goodsid);
        if(empty($welfareInfo)) {
            $this->_exit(40030);
        } else {
            $welfareInfo['joinNum'] = $welfareModel->getJoinNum($welfareInfo['id']);
            if($welfareInfo['condition']) {
                $res = $this->cget(Yii::$app->params['mall_domain'] . 'communityv1/expert/getexpertname?expert_area_id=' . $welfareInfo['condition']);
                $res = json_decode($res, true);
                $welfareInfo['expertName'] = $res['expert_name'];
            } else {
                $welfareInfo['expertName'] = null;
            }
        
            $welfareInfo['endtime'] -= time();
        }
        $this->_exit(0, $welfareInfo);
    }
}
